package com.ts.content.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ts.common.core.utils.Result;
import com.ts.content.Job.HotTpoicJob;
import com.ts.content.constants.RedisKey;
import com.ts.content.dto.TopicDTO;
import com.ts.content.entity.TopicEntity;
import com.ts.content.entity.TopicUserEntity;
import com.ts.content.service.TopicService;
import com.ts.content.service.TopicUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RestController
@RequestMapping("/topic")
@Slf4j
public class TopicController {

    /**
     * 圈子表
     */
    @Autowired
    private TopicService topicService;


    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 用户关注圈子表
     */
    @Autowired
    private TopicUserService topicUserService;

    @Autowired
    private HotTpoicJob hotTpoicJob;



    @PostMapping("/page")
    public IPage<TopicDTO> page(@RequestBody Page<TopicDTO> page) {
        return topicService.getTopicList(page);
    }

    @PostMapping("/save")
    public Result save(@RequestBody TopicEntity entity) {
        log.debug("topicEntity:{}", entity);
        entity.setCreateTime(new Date());
        boolean save = topicService.save(entity);
        //创建了圈子后，默认直接加入圈子
        if (save){
            TopicUserEntity topicUserEntity = new TopicUserEntity();
            topicUserEntity.setTopicId(entity.getId());
            topicUserEntity.setUserId(Integer.valueOf(entity.getUserId()));
            topicUserEntity.setCreateTime(new Date());
        }
        return new Result().viewOk(entity.getId());
    }

    /**
     * 我的圈子接口
     * @param userId
     * @return
     */
    @GetMapping("/myCreateTopic")
    public Result myCreateTopic(@RequestParam Integer userId){
        log.debug("{}:进入了我的圈子接口",userId);
        Map<String,Object> map = new HashMap<>();
        map.put("user_id",userId);
        List<TopicEntity> topicEntities = topicService.listByMap(map);
        return new Result().ok(topicEntities);
    }

    /**
     *根据类目id查出圈子
     * @param classId
     * @return
     */
    @GetMapping("/topiclist")
    public Result topiclist(@RequestParam Integer classId){
        log.debug("{}:作为参数传进了topiclist",classId);
        Map<String, Object> map = new HashMap<>();
        map.put("class_id",classId);
        List<TopicEntity> topiclist = topicService.listByMap(map);
        return new Result().ok(topiclist);
    }

    /**count 热门圈子显示的个数
     * 热门圈子接口
     * @return
     */
    @GetMapping("/hot")
    public Result hot(@RequestParam Integer count){
        log.debug("进入了热门圈子接口");
        List<TopicEntity> hotTopic = topicService.getHotTopic(count);
        return new Result().ok(hotTopic);
    }


    /**count 热门圈子显示的个数
     * 热门圈子接口
     * @return
     */
    @GetMapping("/hotTopic")
    public Result hottopic(@RequestParam Integer count){
        log.debug("进入了热门圈子接口");
        hotTpoicJob.setCount(count);
        Set hottopicset = redisTemplate.opsForSet().members(RedisKey.JOB_SEARCH_HOTTOPIC_KEY);
        List hotTopic = new ArrayList(hottopicset);
        return new Result().ok(hotTopic);
    }



    /**
     * 用户关注的圈子
     * @param userId
     * @return
     */
    @GetMapping("/currentUserJoinTopic")
    public Result currentUserJoinTopic(@RequestParam Integer userId){
        List<TopicEntity> topicEntityList =  topicService.getCurrentUserJoinTopic(userId);
        return new Result().ok(topicEntityList);
    }

    /**
     * 加入
     * @param topicUserEntity
     * @return
     */

    @PostMapping("/joinTopic")
    public Result joinTopic(@RequestBody TopicUserEntity topicUserEntity){
        log.debug("进入了joinTopic");
        boolean save = topicUserService.save(topicUserEntity);
        if (save){
            return new Result().viewOk(200);
        }else {
            return new Result().error(0);
        }
    }

    /**
     * 通过id查询圈子
     * @return
     */
    @GetMapping("/detail")
    public Result detail(@RequestParam Integer id){
        log.debug("进入了查询圈子接口:{}",id);
        TopicEntity topicEntity = topicService.getById(id);
        return new Result().ok(topicEntity);
    }



}
